from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
class Discuz:
    def __init__(self,driver):
        self.driver = driver
    #注册
    def register(self,username,password,email):
        self.driver.find_element(By.LINK_TEXT, '立即注册').click()
        sleep(2)
        self.driver.find_element(By.ID, 'Z3zliw').clear()
        self.driver.find_element(By.ID, 'Z3zliw').send_keys(username)
        sleep(2)
        self.driver.find_element(By.ID, 'mhPHhz').clear()
        self.driver.find_element(By.ID, 'mhPHhz').send_keys(password)
        sleep(2)
        self.driver.find_element(By.ID, 'Avgv3G').clear()
        self.driver.find_element(By.ID, 'Avgv3G').send_keys(password)
        sleep(2)
        self.driver.find_element(By.ID, 'wK4rkZ').clear()
        self.driver.find_element(By.ID, 'wK4rkZ').send_keys(email)
        sleep(2)
        self.driver.find_element(By.ID, 'registerformsubmit').click()
        sleep(3)
    # 登录
    def login(self,username,password):

        self.driver.find_element(By.ID, 'ls_username').clear()
        self.driver.find_element(By.ID, 'ls_username').send_keys(username)
        sleep(1)
        self.driver.find_element(By.ID, 'ls_password').clear()
        self.driver.find_element(By.ID, 'ls_password').send_keys(password)
        sleep(1)
        self.driver.find_elements(By.CLASS_NAME, 'vm')[2].click()
        sleep(2)
        
    # 看帖
    def scan(self,username,password):
        self.login(username,password)
        self.driver.find_elements(By.CLASS_NAME, 'fl_icn')[0].click()
        sleep(3)
        self.driver.find_elements(By.CLASS_NAME, 'xst')[0].click()
        sleep(3)

    # 发帖
    def post(self,username,password):
        self.login(username, password)
        self.driver.find_elements(By.CLASS_NAME, 'fl_icn')[0].click()
        sleep(1)
        self.driver.find_element(By.ID, 'newspecial').click()
        sleep(1)
        self.driver.find_element(By.ID, 'subject').clear()
        self.driver.find_element(By.ID, 'subject').send_keys('我的新发帖标题')
        # 注意：嵌套页面的处理
        self.driver.switch_to.frame('e_iframe')
        sleep(3)
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys('我的新发帖内容，啦啦啦啦啦啦啦啦啦啦')
        # 注意：跳回默认页面
        sleep(3)
        self.driver.switch_to.default_content()
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)
        self.logout()

    #回帖
    def reply(self,username,password):
        self.scan(username, password)
        self.driver.find_element(By.NAME,'message').send_keys('回帖回帖回帖回帖回帖回帖回帖')
        sleep(3)
        self.driver.find_element(By.ID,'fastpostsubmit').click()
        sleep(3)
        self.logout()


    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)
        self.driver.quit()
        
